package com.emapp.lccode.activity

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.os.Bundle
import android.widget.DatePicker
import androidx.fragment.app.Fragment
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ViewPgerKtAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ActivityReportBinding
import com.emapp.lccode.fragment.ProjectProgressAddKtFragment
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.toEntity
import com.emapp.lccode.utils.toJsonStr
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ktx.immersionBar
import fragment.ProjectCompleteAddKtFragment
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 工程上报
 */
class ProjectReportActivity : BaseActivity1<BaseViewModel, ActivityReportBinding>(),
    OnDateSetListener {
    var titles: ArrayList<String> = arrayListOf("进度上报", "竣工资料")
    var fragments: ArrayList<Fragment> = arrayListOf(ProjectProgressAddKtFragment.newInstance(""),
        ProjectCompleteAddKtFragment.newInstance(""))
    private val adapter: ViewPgerKtAdapter by lazy { ViewPgerKtAdapter(this, fragments) }
     var infor: DeviceKt = DeviceKt()
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        //infor= intent.getSerializableExtra("infor3") as DeviceKt
        val inf = MmkvUtil.getString("infor1","")
        if (inf.isNotEmpty()){
            infor= inf.toEntity<DeviceKt>()!!
            //MmkvUtil.putString("infor1","")
        }
        mViewBind.lvTitle.tvTitle.text = "工程上报"
        mViewBind.lvTitle.tvRight.text = "进度"
        EventBus.getDefault().register(this)
        mViewBind.viewpager.adapter = adapter
        mViewBind.viewpager.offscreenPageLimit = 2
        mViewBind.layoutTab.tabMode = TabLayout.MODE_SCROLLABLE
        TabLayoutMediator(mViewBind.layoutTab, mViewBind.viewpager) { tab, position ->
            tab.text = titles[position]
        }.attach()
    }

    override fun createObserver() {
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        MmkvUtil.putString("infor1","")
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }
                R.id.tv_right -> {
                   val intent =Intent(this,ProgressListActivity::class.java)
//                    intent.putExtra("infor", infor)
                    MmkvUtil.putString("infor",infor.toJsonStr())
                    startActivity(intent)
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.PROJECT_DEVICE -> {
               // infor=event.`object` as DeviceKt
            }

            else -> {}
        }
    }
    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        val desc = String.format("%s-%02d-%02d", year, month + 1, dayOfMonth)
    }

}
